package com.liesheng.haylou.ui.main.card;

import android.content.Intent;
import android.view.View;

import com.liesheng.haylou.R;
import com.liesheng.haylou.base.BaseActivity;
import com.liesheng.haylou.base.BaseFunActivity;
import com.liesheng.haylou.base.BaseVm;
import com.liesheng.haylou.bean.CardBean;
import com.liesheng.haylou.databinding.ActivityCardManagmentBinding;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by Android Studio.
 * User: zhangzhiqiang
 * Date: 2019/12/18
 * Time: 9:07
 */

public class CardManagementActivity extends BaseActivity<ActivityCardManagmentBinding, BaseVm> {
    private List<CardBean> mData = new ArrayList<>();
    CardManagerVm cardManagerVm;
    private CardManagerVm.OnItemClickListener onItemClickListener;
    @Override
    protected ActivityCardManagmentBinding getmBinding() {
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_card_managment, null, false);
        return mBinding;
    }

    @Override
    protected BaseVm getViewModel() {
        return null;
    }

    @Override
    protected void initData() {
        showHeadLayout(true);
        setTitle("卡片管理");
        LIstinitData();
        setStatusBarColor(R.color.main_bg, false);
        mBinding.mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        cardManagerVm = new CardManagerVm(mData,onItemClickListener);
        mBinding.mRecyclerView.setAdapter(cardManagerVm);
        //为RecycleView绑定触摸事件
        cardManagerVm.setOnItemClickListener(new CardManagerVm.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                mData.remove(position);
                cardManagerVm.notifyDataSetChanged();
                cardManagerVm.notifyItemRemoved(position);
            }
        });
        ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                //首先回调的方法 返回int表示是否监听该方向
                int dragFlags = ItemTouchHelper.UP|ItemTouchHelper.DOWN;//拖拽
                int swipeFlags = ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;//侧滑删除

                return makeMovementFlags(dragFlags,swipeFlags);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                //滑动事件
                Collections.swap(mData,viewHolder.getAdapterPosition(),target.getAdapterPosition());
                cardManagerVm.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                //侧滑删除Item
                mData.remove(viewHolder.getAdapterPosition());
                cardManagerVm.notifyItemRemoved(viewHolder.getAdapterPosition());
            }

            @Override
            public boolean isLongPressDragEnabled() {
                //是否可拖拽
                return true;
            }

        });
        helper.attachToRecyclerView(mBinding.mRecyclerView);
    }

    public static void startBy(BaseFunActivity activity) {
        Intent intent = new Intent(activity, CardManagementActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public void onClickView(View view) {

    }
    private void LIstinitData() {
        mData = new ArrayList<>();
        mData.add(new CardBean(R.drawable.home_icon_cardopt_reduce, R.drawable.home_ico_sport,"运动",  R.drawable.home_ico_sort));
        mData.add(new CardBean(R.drawable.home_icon_cardopt_reduce, R.drawable.home_ico_plan,"跑步计划", R.drawable.home_ico_sort));
        mData.add(new CardBean(R.drawable.home_icon_cardopt_reduce,R.drawable.home_ico_plan,"我的音效",  R.drawable.home_ico_sort));
        mData.add(new CardBean(R.drawable.home_icon_cardopt_reduce, R.drawable.home_ico_heartrate,"心率",  R.drawable.home_ico_sort));
        mData.add(new CardBean(R.drawable.home_icon_cardopt_reduce, R.drawable.home_ico_sleep,"睡眠",  R.drawable.home_ico_sort));
        mData.add(new CardBean(R.drawable.home_icon_cardopt_reduce, R.drawable.home_ico_psv,"心理压力值", R.drawable.home_ico_sort));
        mData.add(new CardBean(R.drawable.home_ico_cardopt_add, R.drawable.home_ico_weight,"体重", R.drawable.home_ico_sort));
        mData.add(new CardBean(R.drawable.home_ico_cardopt_add, R.drawable.home_ico_bos,"血氧饱和度", R.drawable.home_ico_sort));
    }
}
